package com.example.service;


import com.example.mapper.DatabaseTableMapper;
import com.example.model.SDUser;
import com.example.mapper.TableMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 分库分表测试
 */
@Service
public class TestService {

    @Resource
    private TableMapper tableMapper;


    @Resource
    private DatabaseTableMapper databaseTableMapper;

    public Object table() {
        // 插入测试
        for (int i = 1; i <= 10; i++) {
            SDUser user = new SDUser();
            user.setId(i);
            user.setName("Name_" + i);
            user.setPhone("phone");
            user.setEmail("email");

            tableMapper.insertTable(user);
        }

        // 修改测试(模拟修改Id为1的数据)
        tableMapper.updateTableById(1L, "修改名称");

        // 删除测试(模拟修改Id为2的数据)
        tableMapper.deleteTableById(2L);

        // 查询测试
        List<SDUser> list = tableMapper.listAllTable();

        return list;
    }


    /**
     * 分库分表测试
     *
     * @return
     */
    public Object databaseTable() {
        // 插入测试
        for (int i = 1; i <= 10; i++) {
            //模拟ID
            int id = (int) (Math.random() * 100000);
            SDUser user = new SDUser();
            user.setId(id);
            user.setName("Name_" + i);
            user.setPhone("phone");
            user.setEmail("email");
            databaseTableMapper.insertDatabaseUser(user);

             /*   Account account = new Account();
                account.setId(id);
                account.setUserId(user.getId());
                account.setUserName("UserName_" + i);

                databaseTableMapper.insertDatabaseAccount(account);*/
        }

        // 修改测试(模拟修改Id为1的数据)
        databaseTableMapper.updateDatabaseUserById(100L, "修改名称");

        // 删除测试(模拟修改Id为2的数据)
        databaseTableMapper.deleteDatabaseUserById(200L);

           /* // 修改测试(模拟修改Id为1的数据)
            databaseTableMapper.updateDatabaseAccountById(100L, "修改名称");

            // 删除测试(模拟修改Id为2的数据)
            databaseTableMapper.deleteDatabaseAccountById(200L);
*/
        // 查询测试
        List<SDUser> userLIst = databaseTableMapper.listAllDatabaseUser();
        //List<Account> accountList = databaseTableMapper.listAllDatabaseAccount();

        Map<String, Object> map = new HashMap<>();
        map.put("user", userLIst);
        //map.put("account", accountList);

        return map;
    }


}
